/**
 *  编辑页
 *
 * 作者 admin
 * 日期 2024-08-07 16:43:01
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        laydate = layui.laydate,
        form = layui.form,
        $view = $('#reportRectificationAssign-edit'),
        $accidentType = $('div[name="accidentType"]'),
        $rectificationDeptName = $('#rectificationDeptName'),
        validate = layui.validate;

    initDictItemLabel_reportBaseInfo();

    form.verify(validate);
    form.render();
    initFormValue();


    laydate.render({
        elem: "#rectificationDeadline",
        done: function (value, date) {},
        type: 'date'
    })

    function initDictItemLabel_reportBaseInfo(){
        febs.initDictItemLabel($accidentType,'SAFETY_ACCIDENT_TYPE',accidentReportData.accidentType);
    }

    $rectificationDeptName.on('click', function () {
        febs.modal.open('整改部门及负责人选择', 'common/user', {
            btn: ['确认','关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%']: ['80%', '85%'],
            data:{
                tenantId:'0',
                multiple:false,
            },
            yes: function (index, layero) {
                let selectData = null
                if(typeof getSelectData  === 'function'){
                    selectData = getSelectData()
                }
                setRectifyUserAndDeptAttr(selectData)
                layer.close(index);
            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });
    });

    function setRectifyUserAndDeptAttr(selectData){
        $view.find("input[name=rectificationDeptId]").val(selectData.deptId);
        $view.find("input[name=rectificationDeptName]").val(selectData.deptName);
        $view.find("#rectificationDeptNameDiv").text(selectData.deptName);
        $view.find("input[name=rectificationUserId]").val(selectData.userId);
        $view.find("input[name=rectificationUserName]").val(selectData.userName);
        $view.find("input[name=rectificationRealName]").val(selectData.uname);
    }
    function initFormValue() {
        if (accidentReportData) {
            form.val("reportRectificationAssign-edit-form", {
                "reportId": accidentReportData.reportId,
                "tenantId": accidentReportData.tenantId,
                "accidentNo": accidentReportData.accidentNo,
                "accidentType": accidentReportData.accidentType,
                "accidentTitle": accidentReportData.accidentTitle,
                "accidentDate": accidentReportData.accidentDate,
                "rectificationStatus": accidentReportData.rectificationStatus,
                "rectificationDeadline": accidentReportData.rectificationDeadline,
                "rectificationDeptId": accidentReportData.rectificationDeptId,
                "rectificationDeptName": accidentReportData.rectificationDeptName,
                "rectificationUserId": accidentReportData.rectificationUserId,
                "rectificationUserName": accidentReportData.rectificationUserName,
                "rectificationRealName": accidentReportData.rectificationRealName,
                "rectificationRemark": accidentReportData.rectificationRemark,
                "rectificationDate": accidentReportData.rectificationDate,
                "rectificationMeasures": accidentReportData.rectificationMeasures,
                "educationCondition": accidentReportData.educationCondition,
                "rectificationAuditUserId": accidentReportData.rectificationAuditUserId,
                "rectificationAuditRealName": accidentReportData.rectificationAuditRealName,
                "rectificationAuditDate": accidentReportData.rectificationAuditDate,
                "rectificationAuditRemark": accidentReportData.rectificationAuditRemark,
                "status": accidentReportData.status,
            });
        }
    }

    form.on('submit(reportRectificationAssign-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });


    function addOrUpdate(data) {
        data.tenantId = POPUP_DATA.tenantId;
        if (data.reportId && data.reportId.length > 0) {
            febs.modal.confirm('提交整改指派信息','确定提交整改指派信息？', function (index) {
                febs.postArray(ctx + 'accidentReport/rectificationAssign', data, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                    $('#febs-accidentReport').find('#query').trigger('click');
                });
            });
        }
    }
});